package com.app.twth.ui.adjust;


import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityAdjustBinding;
import com.app.twth.ui.adjust.lpn.AdjustSkuActivity;
import com.app.twth.ui.adjust.remeasure.ReMeasureActivity;
import com.app.twth.ui.adjust.retally.ReTallyctivity;
import com.app.twth.utils.EditTextUtil;

/**
 * @describe :
 */
public class AdjustActivity extends BaseActivity<ActivityAdjustBinding, AdjustViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 2;


    @Override
    protected void initListener() {
        viewModel.LpnSkuLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showFragmentActivity(mContext, AdjustSkuActivity.class, bundle);
        });
        viewModel.reTallyLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showFragmentActivity(mContext, ReTallyctivity.class, bundle);
        });
        viewModel.reMeasureLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", viewModel.reMeasureLiveData.getValue());
            AppRouter.showFragmentActivity(mContext, ReMeasureActivity.class, bundle);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint(code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Adjust";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.rb_lpn:
                if (type == 0) {
                    return;
                }
                type = 0;
                resetView(getString(R.string.scan_inbound_lpn_lower));
                break;
            case R.id.rb_location:
                if (type == 1) {
                    return;
                }
                type = 1;
                resetView(getString(R.string.scan_location_barcode_lower));
                break;
            case R.id.rb_re_tally:
                if (type == 2) {
                    return;
                }
                type = 2;
                resetView(getString(R.string.scan_inbound_lpn_lower));
                break;
            case R.id.rb_re_measure:
                if (type == 3) {
                    return;
                }
                type = 3;
                resetView(getString(R.string.scan_sku_barcode_lower));
                break;
            case R.id.rb_returns:
                if (type == 4) {
                    return;
                }
                type = 4;
                resetView(getString(R.string.scan_inbound_lpn_returns_lower));
                break;
        }
    }

    private void resetView(String code_hint) {
        binding.tvCode.setText("");
        binding.tvCode.requestFocus();
        binding.tvCode.setHint(code_hint);

        binding.rbLpn.setChecked(type == 0 ? true : false);
        binding.rbLocation.setChecked(type == 1 ? true : false);
        binding.rbReTally.setChecked(type == 2 ? true : false);
        binding.rbReMeasure.setChecked(type == 3 ? true : false);
        binding.rbReturns.setChecked(type == 4 ? true : false);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                setInventory();
            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        setInventory();
    }

    private void setInventory() {
        bottomLayoutBinding.tvHint.setText("");
        //如果移到末尾，那么index则传入 editText.getText().length()
        EditTextUtil.setSelection(binding.tvCode);
        switch (type) {
            case 0:
                viewModel.adjustLpn(barcodeStr);
                break;
            case 1:
//                getPresenter().getInventory(type, barcodeStr);
                break;
            case 2:
                viewModel.tallyReTally();
                break;
            case 3:
                viewModel.tallyReMeasure();
                break;
            case 4:

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
